package com.itheima.sfbx.insurance.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.insurance.dto.ConditionVO;
import com.itheima.sfbx.insurance.mapper.ConditionMapper;
import com.itheima.sfbx.insurance.pojo.CategoryCondition;
import com.itheima.sfbx.insurance.pojo.Condition;
import com.itheima.sfbx.insurance.pojo.InsuranceCondition;
import com.itheima.sfbx.insurance.service.ICategoryConditionService;
import com.itheima.sfbx.insurance.service.IConditionService;
import com.itheima.sfbx.insurance.service.IInsuranceConditionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
/**
 * @Description：筛选项服务实现类
 */
@Slf4j
@Service
public class ConditionServiceImpl extends ServiceImpl<ConditionMapper, Condition> implements IConditionService {



    @Override
    public Page<ConditionVO> findPage(ConditionVO conditionVO, int pageNum, int pageSize) {
        Page<Condition> page = this.lambdaQuery()
                .eq(StrUtil.isNotBlank(conditionVO.getConditionKey()), Condition::getConditionKey, conditionVO.getConditionKey())
                .like(StrUtil.isNotBlank(conditionVO.getConditionKeyName()), Condition::getConditionKeyName, conditionVO.getConditionKeyName())
                .eq(StrUtil.isNotBlank(conditionVO.getDataState()), Condition::getDataState, conditionVO.getDataState())
                .page(new Page<>(pageNum,pageSize));
        if (ObjectUtil.isEmpty(page)){
            throw new RuntimeException("page为空");
        }
        return BeanConv.toPage(page, ConditionVO.class);
    }

    @Override
    public ConditionVO findById(String conditionId) {
        Condition condition = this.getById(conditionId);
        if (ObjectUtil.isEmpty(condition)){
            throw new RuntimeException("筛选项不存在");
        }
        return BeanConv.toBean(condition, ConditionVO.class);
    }

    @Override
    @Transactional
    public ConditionVO save(ConditionVO conditionVO) {
        Integer count = this.lambdaQuery()
                .eq(Condition::getConditionKey, conditionVO.getConditionKey())
                .count();
        if (count > 0){
            throw new RuntimeException("筛选项已存在");
        }
        Condition bean = BeanConv.toBean(conditionVO, Condition.class);
        boolean save = this.save(bean);
        if (!save){
            throw new RuntimeException("操作失败");
        }
        return BeanConv.toBean(bean, ConditionVO.class);
    }

    @Autowired
    private IInsuranceConditionService insuranceConditionService;

    @Autowired
    private ICategoryConditionService categoryConditionService;

    @Override
    @Transactional
    public Boolean update(ConditionVO conditionVO) {
        Condition condition = this.getById(conditionVO.getId());
        if (ObjectUtil.isEmpty(condition)){
            throw new RuntimeException("筛选项不存在，无法修改");
        }
        Integer count1 = insuranceConditionService.lambdaQuery()
                .eq(InsuranceCondition::getConditionKey, condition.getConditionKey())
                .count();
        Integer count2 = categoryConditionService.lambdaQuery()
                .eq(CategoryCondition::getConditionKey, condition.getConditionKey())
                .count();
        if (count1>0 || count2>0){
            throw new RuntimeException("筛选项存在绑定关系，无法修改");
        }
        return this.updateById(BeanConv.toBean(conditionVO, Condition.class));
    }

    @Override
    @Transactional
    public Boolean delete(String[] checkedIds) {
        List<String> idList=new ArrayList<>();
        for (String checkedId : checkedIds) {
            Condition condition = this.getById(checkedId);
            if (ObjectUtil.isEmpty(condition)){
                throw new RuntimeException("筛选项不存在");
            }
            Integer count1 = insuranceConditionService.lambdaQuery()
                    .eq(InsuranceCondition::getConditionKey, condition.getConditionKey())
                    .count();
            Integer count2 = categoryConditionService.lambdaQuery()
                    .eq(CategoryCondition::getConditionKey, condition.getConditionKey())
                    .count();
            if (count1>0 || count2>0){
                throw new RuntimeException("筛选项存在绑定关系，无法删除");
            }
            idList.add(checkedId);
        }
        return this.removeByIds(idList);
    }

    @Override
    public List<ConditionVO> findList(ConditionVO conditionVO) {
        List<Condition> list = this.lambdaQuery()
                .eq(StrUtil.isNotBlank(conditionVO.getConditionKey()), Condition::getConditionKey, conditionVO.getConditionKey())
                .like(StrUtil.isNotBlank(conditionVO.getConditionKeyName()), Condition::getConditionKeyName, conditionVO.getConditionKeyName())
                .eq(StrUtil.isNotBlank(conditionVO.getDataState()), Condition::getDataState, conditionVO.getDataState())
                .list();
        return BeanConv.toBeanList(list, ConditionVO.class);
    }

    @Override
    public List<ConditionVO> findInConditionKeys(List<String> conditionKeys) {
        List<Condition> conditions = this.listByIds(conditionKeys);
        if (CollUtil.isEmpty(conditions)){
            throw new RuntimeException("集合为空");
        }
        return BeanConv.toBeanList(conditions, ConditionVO.class);
    }
}
